package android.medicalcloud.com.yiapp.yilian.ui.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.medicalcloud.com.yiapp.R;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;



import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by User on 2016/12/4.
 */
public class CommonTitleView extends LinearLayout {
    Context context;
    ImageView backImage;
    TextView titleText;

    private OnRightClickListener onRightClickListener;
    private OnLeftClickListener onLeftClickListener;

    public interface OnRightClickListener {
        void onRightClick();
    }

    public interface OnLeftClickListener {
        void onLeftClick();
    }

    public void setOnRightClickListener(OnRightClickListener onRightClickListener) {
        this.onRightClickListener = onRightClickListener;
    }

    public void setOnLeftClickListener(OnLeftClickListener onLeftClickListener) {
        this.onLeftClickListener = onLeftClickListener;
    }

    public CommonTitleView(Context context) {
        this(context, null);
    }

    public CommonTitleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CommonTitleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView(attrs);
    }

    private void initView(AttributeSet attrs) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_common_title, this);
        backImage = (ImageView)view.findViewById(R.id.backImage);
        titleText = (TextView) view.findViewById(R.id.titleText);

        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.CommonTitleView);
        boolean showBack = attributes.getBoolean(R.styleable.CommonTitleView_showBack, true);

        String title = attributes.getString(R.styleable.CommonTitleView_titleText);
        backImage.setVisibility(showBack ? VISIBLE : GONE);


        titleText.setText(title);
        backImage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onLeftClickListener!=null)

                {onLeftClickListener.onLeftClick();}else{((Activity) context).onBackPressed();}

            }
        });
        attributes.recycle();

    }

    public void setTitleText(String title){
        titleText.setText(title);
    }
}
